//
//  CLHCollectionViewVC.m
//  ListViewModel_Example
//
//  Created by 曹连华 on 2019/7/31.
//  Copyright © 2019 clh2046@163.com. All rights reserved.
//

#import <Masonry/Masonry.h>
#import "CLHCollectionViewCVCell.h"
#import "CLHCollectionSectionHeaderView.h"

#import "CLHCollectionViewVC.h"
#import "CLHCollectionViewVCViewModel.h"
#import "CLHCollectionSectionFooterView.h"


@interface CLHCollectionViewVC ()

@property (nonatomic, strong) CLHCollectionViewVCViewModel *viewModel;

@end

@implementation CLHCollectionViewVC

@synthesize viewModel = _viewModel;

- (CLHCollectionViewVCViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[CLHCollectionViewVCViewModel alloc]init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initUI];
        
}

- (void)initUI {
    self.navigationItem.title = @"CollectionView";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:CLHCollectionViewCVCell.class forCellWithReuseIdentifier:CLHCollectionViewCVCell.reuseIdentifier];
    
    [self.collectionView registerClass:CLHCollectionSectionHeaderView.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CLHCollectionSectionHeaderView.reuseIdentifier];

    [self.collectionView registerClass:CLHCollectionSectionFooterView.class forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:CLHCollectionSectionFooterView.reuseIdentifier];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(0);
        make.bottom.top.mas_equalTo(0);
    }];
    
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    LVBaseCellSectionModel *sectionModel = [self.viewModel.dataSource sectionModelAtIndex:indexPath.section];
        LVBaseSectionHeaderFooterModel *headerModel = sectionModel.sectionHeader;
        LVBaseSectionHeaderFooterModel *footerModel = sectionModel.sectionFooter;
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            CLHCollectionSectionHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerModel.reuseIdentifier forIndexPath:indexPath];
            [header updateMainTitle:headerModel.headerFooterTitle subTitle:@"CLHCollectionSectionHeaderView"];
            return header;

        } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
            CLHCollectionSectionFooterView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerModel.reuseIdentifier forIndexPath:indexPath];
            [footer updateMainTitle:footerModel.headerFooterTitle subTitle:@"CLHCollectionSectionFooterView"];
            return footer;

        }
    return nil;
}

- (nullable NSArray<NSString *> *)indexTitlesForCollectionView:(UICollectionView *)collectionView {
    return @[@"1",@"2",@"3",@"4",@"5"];
}

@end
